package com.example.conf;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class RedisConfiguration {
    // 导入的依赖中已经创建好了RedisConnectionFactory对象在IOC容器中,声明就能注入
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        log.info("开始创建redis模板对象....");
        RedisTemplate redisTemplate = new RedisTemplate();
        // 设置redis的连接对象功能
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 设置redis key序列化器,不设置会使用默认的序列化器，能使可视化程序中的key更好理解，value依旧乱码
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}
